package org.wsff.tools.api.gateway.error;

/**
 * 网关异常基类
 *
 * @author ryan
 * @version Id: GatewayException.java, v 0.1 2022-01-18 14:44 ryan Exp $$
 */
public class GatewayException extends Exception {

    private static final long serialVersionUID = 535819537907696906L;

    /**错误消息对象*/
    private GatewayError      i18nError;

    /**
     * Constructs a new exception with the specified detail message.  The
     * cause is not initialized, and may subsequently be initialized by
     * a call to {@link #initCause}.
     *
     * @param   message   the detail message. The detail message is saved for
     *          later retrieval by the {@link #getMessage()} method.
     */
    public GatewayException(String message) {
        super(message);
        setError(AbstractErrorMessageFactory.createStaticError(GatewayError.DEFAULT_ERROR_CODE, message));
    }

    /**
     * Constructs a new exception with {@code null} as its detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     */
    public GatewayException(GatewayError i18nError) {
        super(i18nError.getMessage());
        this.i18nError = i18nError;
    }

    /**
     * Constructs a new exception with the specified cause and a detail
     * message of <tt>(cause==null ? null : cause.toString())</tt> (which
     * typically contains the class and detail message of <tt>cause</tt>).
     * This constructor is useful for exceptions that are little more than
     * wrappers for other throwables
     *
     * @param  cause the cause (which is saved for later retrieval by the
     *         {@link #getCause()} method).  (A <tt>null</tt> value is
     *         permitted, and indicates that the cause is nonexistent or
     *         unknown.)
     * @since 1.4
     */
    public GatewayException(Throwable cause, GatewayError i18nError) {
        super(ExceptionHelper.unwrap(cause));
        this.i18nError = i18nError;
    }

    /**
     * Constructs a new exception with the specified cause and a detail
     * message of <tt>(cause==null ? null : cause.toString())</tt> (which
     * typically contains the class and detail message of <tt>cause</tt>).
     * This constructor is useful for exceptions that are little more than
     * wrappers for other throwables
     *
     * @param  cause the cause (which is saved for later retrieval by the
     *         {@link #getCause()} method).  (A <tt>null</tt> value is
     *         permitted, and indicates that the cause is nonexistent or
     *         unknown.)
     * @since 1.4
     */
    public GatewayException(Throwable cause) {
        super(ExceptionHelper.unwrap(cause));
        if (cause != null) {
            setError(AbstractErrorMessageFactory.createStaticError(GatewayError.DEFAULT_ERROR_CODE, cause.getMessage() + " (" + cause.getClass().getName() + ")"));
        }
    }

    public static GatewayException create(String code, String msg, Exception e) {
        GatewayException exception = new GatewayException(ExceptionHelper.unwrap(e));
        exception.setError(AbstractErrorMessageFactory.createStaticError(code, msg));
        return exception;
    }

    public static GatewayException create(String msg) {
        GatewayException exception = new GatewayException(msg);
        exception.setError(AbstractErrorMessageFactory.createStaticError(GatewayError.DEFAULT_ERROR_CODE, msg));
        return exception;
    }

    public static GatewayException create(String msg, Exception e) {
        GatewayException exception = new GatewayException(ExceptionHelper.unwrap(e));
        exception.setError(AbstractErrorMessageFactory.createStaticError(GatewayError.DEFAULT_ERROR_CODE, msg));
        return exception;
    }

    /**
     * 设置异常的错误消息
     *
     * 一般来说，一条错误消息针对资源文件中的一条配置记录
     * 错误码＝错误描述
     *
     * @param error 资源文件描述的错误消息
     */
    protected void setError(GatewayError error) {
        this.i18nError = error;
    }

    /**
     * Gets the value of i18nError.
     *
     * @return the value of i18nError
     */
    public GatewayError getI18nError() {
        return i18nError;
    }

    /**
     * 获取错误消息的错误码
     *
     * @return code
     */
    public String getErrorCode() {
        return i18nError == null ? GatewayError.DEFAULT_ERROR_CODE : i18nError.getErrorCode();
    }

    /**
     * 获取错误消息的错误明细
     *
     * @return code
     */
    public String getErrorMessage() {
        return i18nError == null ? getMessage() : i18nError.getErrorMessage();
    }

}
